package com.zdj;


/**
 * @author zhangdj
 * @date 2021-10-12 18:02:47
 * 判断一颗二叉树是否是对称的
 */
public class IsSymmetric {

    public static void main(String[] args) {

    }

    public static boolean isSymmetric(TreeNode root) {
        if (root == null) {
            return true;
        }
        return symmetric(root.left, root.right);
    }

    public static boolean symmetric(TreeNode t1, TreeNode t2) {
        if (t1 == null && t2 == null) {
            return true;
        }

        //
        if (t1 == null || t2 == null || t1.val != t2.val) {
            return false;
        }
        return symmetric(t1.left, t2.right) && symmetric(t1.right, t2.left);
    }

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }
}
